﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCH.Areas.Admin.Models;
using DCH.Models;
using System.IO;

namespace DCH.Areas.Admin.Controllers
{
      [Authorize]
    public class PlayerController : Controller
    {
        private DbConnection db = new DbConnection();

        //
        // GET: /Admin/Player/

        public ActionResult Index()
        {
            return View(db.Player.ToList());
        }

        //
        // GET: /Admin/Player/Details/5

        public ActionResult Details(int id = 0)
        {
            Player player = db.Player.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // GET: /Admin/Player/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Player/Create
         [ValidateInput(false)] 
        [HttpPost] 
        public ActionResult Create(Player player, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null)
                {
                    string pic = Guid.NewGuid().ToString().Substring(0, 4) + System.IO.Path.GetFileName(upload.FileName);
                    string path = System.IO.Path.Combine(
                                           Server.MapPath("~/images/player"),   pic);
                    // file is uploaded
                    upload.SaveAs(path);
                    player.image = pic;
                }
                db.Player.Add(player);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(player);
        }

        //
        // GET: /Admin/Player/Edit/5

        public ActionResult Edit( int id = 0 )
        {
            Player player = db.Player.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // POST: /Admin/Player/Edit/5

        [HttpPost]
        public ActionResult Edit(HttpPostedFileBase upload, Player player)
        {
            if (ModelState.IsValid)
            {

                if (upload != null)
                {
                    string pic = Guid.NewGuid().ToString().Substring(0, 4) + System.IO.Path.GetFileName(upload.FileName);
                    string path = System.IO.Path.Combine(
                                           Server.MapPath("~/images/player"), pic);
                    // file is uploaded
                    upload.SaveAs(path);
                    player.image = pic;
                }
                else
                    player.image = db.Player.Find(player.ID).image;
                db.Entry(player).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(player);
        }

        //
        // GET: /Admin/Player/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Player player = db.Player.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // POST: /Admin/Player/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Player player = db.Player.Find(id);
            db.Player.Remove(player);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}